package com.my.servlets;

import com.my.entity.Page;
import com.my.entity.User;
import com.my.user.service.IUserService;
import com.my.user.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class QueryUsersByPageServlet  extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String cPageSize = request.getParameter("selPageSize");
        if(cPageSize==null){
            cPageSize = "3";
        }
        IUserService service = new UserServiceImpl();
        Page p = new Page();
        int pageSize = Integer.parseInt(cPageSize);
        //要注意设置page属性的顺序
        int count = service.getTotalCount();
        p.setTotalCount(count);
        p.setPageSize(pageSize);

        String cPage = request.getParameter("currentPage");
        if(cPage==null){
            cPage="1";
        }
        int currentPage =Integer.parseInt(cPage);
        p.setCurrentPage(currentPage);

        List<User> users = service.queryUserByPage(p.getCurrentPage(),pageSize);

        p.setUsers(users);
        request.setAttribute("page",p);
        request.getRequestDispatcher("ShowUsersByPage.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
